package com.jingdianjichi.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.enums.IsDeletedFlageEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.file.OpenOption;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //单选题目的插入
        Preconditions.checkNotNull(subjectInfoBo.getOptionList(), "答案选项为空");
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        subjectInfoBo.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE
                    .convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBo.getId());
            subjectRadio.setOptionType(option.getOptionType());
            subjectRadio.setOptionContent(option.getOptionContent());
            subjectRadio.setIsCorrect(option.getIsCorrect());
            subjectRadio.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBo queue(Long subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        subjectRadio.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
        List<SubjectRadio> radioResult=   subjectRadioService.queryByCondition(subjectRadio);

        List<SubjectAnswerBo> subjectAnswerBos = RadioSubjectConverter.INSTANCE
                .convertRadioListToBoAnswerBoList(radioResult);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBos);
        return subjectOptionBo;
    }
}
